document.addEventListener('DOMContentLoaded', function() {
    // 获取搜索输入框和搜索按钮
    const searchInput = document.getElementById('searchInput');

    // 监听搜索输入框的回车事件
    searchInput.addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            performSearch();
        }
    });

    // 如果有初始搜索值，显示清除按钮
    if (searchInput.value) {
        showClearButton();
    }
});

// 执行搜索
function performSearch() {
    const searchValue = document.getElementById('searchInput').value.trim();
    const baseUrl = '/sale';

    // 构建带搜索参数的URL
    let url = baseUrl;
    if (searchValue) {
        url += '?name=' + encodeURIComponent(searchValue);
    }

    // 跳转到带搜索参数的URL
    window.location.href = url;
}

// 显示清除搜索按钮
function showClearButton() {
    const searchBox = document.querySelector('.search-box');
    const clearButton = document.createElement('a');
    clearButton.innerHTML = '&times;';
    clearButton.href = '/sale';
    clearButton.title = '清除搜索条件';
    clearButton.style.marginLeft = '5px';
    clearButton.style.cursor = 'pointer';
    clearButton.style.color = '#999';
    searchBox.appendChild(clearButton);
}